package com.qq2008.game.mojie.controller;

import com.qq2008.game.mojie.jdo.Chat;
import com.qq2008.game.mojie.jdo.Role;
import com.qq2008.game.mojie.server.RoleServer;
import com.qq2008.game.mojie.service.ChatService;
import com.qq2008.game.mojie.service.impl.ChatServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/mojie")
@Slf4j
public class ChatController {
    private static int count = 0;

    @Resource
    private ChatServiceImpl chatService;


    /**
     * 聊天
     *
     * @param model
     * @param session
     * @return
     */
    @RequestMapping("chat")
    public String chat(Model model, HttpSession session) {
        List<Chat> chats = chatService.selectChat(model, session);
        SimpleDateFormat df = new SimpleDateFormat("HH:mm");//设置日期格式
        String format = df.format(new Date());
        model.addAttribute("timeNow", format);
        model.addAttribute("chats", chats);
        return "chat";
    }

    @ResponseBody
    @PostMapping("chatSend")
    public String chatSend(Model model, HttpSession session, String talk) {
        SimpleDateFormat df = new SimpleDateFormat("HH:mm");//设置日期格式
        SimpleDateFormat df2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        Role role = getSessionRole(session);
        Chat chat =Chat.builder()
                .talkContent(talk)
                .talkName(role.getName())
                .talkId(String.valueOf(role.getUserId()))
                .talkTime(df2.format(new Date()))
                .build();
        chatService.insertChat(chat);
        model.addAttribute("timeNow", df.format(new Date()));
        List<Chat> chats = chatService.selectChat(model, session);
        for (Chat chat1 : chats) {
            String format1 = null;
            try {
                format1 = df.format(df2.parse(chat1.getTalkTime()) );
            } catch (ParseException e) {
               log.error("聊天时间转换{}",e.getMessage());
            }
            chat1.setTalkTime(format1);
        }
        model.addAttribute("chats", chats);
        return "chat";
    }


    @RequestMapping("notEnough")
    public String shop(Model model, HttpSession session) {
        SimpleDateFormat df = new SimpleDateFormat("HH:mm");//设置日期格式
        String format = df.format(new Date());
        Role role = getSessionRole(session);
        model.addAttribute("roleName", role.getName());
        model.addAttribute("timeNow", format);
        return "notEnough";
    }


    private Role getSessionRole(HttpSession session) {
        return RoleServer.getRole((int) session.getAttribute("roleId"));
    }

}
